package com.example.tabtestwithsherlock;

import android.app.FragmentTransaction;

import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
  
public class TabListener<T extends SherlockFragment>  
        implements com.actionbarsherlock.app.ActionBar.TabListener {  
    private final SherlockFragmentActivity mActivity;  
    private final String mTag;  
    private final Class<T> mClass;  
  
    private SherlockFragment mFragment;  
  
    public TabListener(SherlockFragmentActivity activity,  
            String tag, Class<T> clz) {  
        mActivity = activity;  
        mTag = tag;  
        mClass = clz;  
    }  
  

	@Override
	public void onTabSelected(Tab tab,
			android.support.v4.app.FragmentTransaction ft) {
        SherlockFragment preInitializedFragment = (SherlockFragment)  
                mActivity.getSupportFragmentManager().findFragmentByTag(mTag);  
        if (preInitializedFragment == null) {  
            mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity,  
                    mClass.getName());  
            ft.add(android.R.id.content, mFragment, mTag);  
        } else {  
            ft.attach(preInitializedFragment);  
        }  
		
	}

	@Override
	public void onTabUnselected(Tab tab,
			android.support.v4.app.FragmentTransaction ft) {
        SherlockFragment preInitializedFragment = (SherlockFragment)  
                mActivity.getSupportFragmentManager().findFragmentByTag(mTag);  
  
        if (preInitializedFragment != null) {  
            ft.detach(preInitializedFragment);  
        } else if (mFragment != null) {  
            ft.detach(mFragment);  
        }  
		
	}

	@Override
	public void onTabReselected(Tab tab,
			android.support.v4.app.FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}  
} 